/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.Image;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.ImageRepository;
import me.zhengjie.modules.system.service.ImageService;
import me.zhengjie.modules.system.service.dto.ImageDto;
import me.zhengjie.modules.system.service.dto.ImageQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.ImageMapper;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author whj
* @date 2021-01-16
**/
@Service
@RequiredArgsConstructor
public class ImageServiceImpl implements ImageService {

    private final ImageRepository imageRepository;
    private final ImageMapper imageMapper;

    @Override
    public Map<String,Object> queryAll(ImageQueryCriteria criteria, Pageable pageable){
        Page<Image> page = imageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(imageMapper::toDto));
    }

    @Override
    public List<ImageDto> queryAll(ImageQueryCriteria criteria){
        return imageMapper.toDto(imageRepository.findAll(((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)),Sort.by(Sort.Direction.ASC,"status","imageName")));
    }

    @Override
    @Transactional
    public ImageDto findById(Long id) {
        Image image = imageRepository.findById(id).orElseGet(Image::new);
        ValidationUtil.isNull(image.getImageId(),"Image","id",id);
        return imageMapper.toDto(image);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ImageDto create(Image resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setImageId(snowflake.nextId());
        return imageMapper.toDto(imageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Image resources) {
        Image image = imageRepository.findById(resources.getImageId()).orElseGet(Image::new);
        ValidationUtil.isNull( image.getImageId(),"Image","id",resources.getImageId());
        image.copy(resources);
        imageRepository.save(image);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            imageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ImageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ImageDto image : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("图片名(带后缀名)", image.getImageName());
            map.put("图片绝对路径", image.getIamgePath());
            map.put("标注json的框数量(初始为0)", image.getNumBox());
            map.put("标注结果json路径", image.getResultPath());
            map.put("0未标注/1标注部分/2等待审核/3审核通过/4审核不通过", image.getStatus());
            map.put("上传标注的时间", image.getUploadJsonTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}